﻿using HDSmartCommunity.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HDSmartCommunity.DAL
{
    public class DeviceDAL
    {
        public DevInfo QueryDev()
        {
            string strSql = "SELECT * from devinfo limit 0,1";
            CommFN<DevInfo> fn = new CommFN<DevInfo>();
            return fn.GetData(strSql);
        }

        public bool UpdateDevInfo(DevInfo device)
        {
            string strSql = string.Format("UPDATE devinfo SET liftid={0},serverIP='{1}',serverport={2},protocoltype='{3}',mainIP='{4}',mainPort={5},enable={6} WHERE ID={7}", device.liftid, device.serverIP, device.serverport, device.protocoltype, device.mainIP, device.mainPort, device.enable, device.ID);
            CommFN<DevInfo> fn = new CommFN<DevInfo>();
            return fn.ExecFN(strSql);
        }

        public bool UpdateSwithInfo(DevInfo device)
        {
            string strSql = string.Format("UPDATE devinfo SET printlog={0},ttrAMA3_send={1},ttrAMA3_receive={2},ttrAMA1_receive={3},ttrAMA2_receive={4} WHERE ID={5}", device.printlog, device.ttrAMA3_send, device.ttrAMA3_receive, device.ttrAMA1_receive, device.ttrAMA2_receive, device.ID);
            CommFN<DevInfo> fn = new CommFN<DevInfo>();
            return fn.ExecFN(strSql);
        }

        public bool InsertDevInfo(DevInfo device)
        {
            string strSql = string.Format("INSERT INTO devinfo (liftid,serverIP,serverport,protocoltype,mainIP,mainPort,enable)VALUES({0},'{1}',{2},'{3}','{4}',{5},{6})", device.liftid, device.serverIP, device.serverport, device.protocoltype, device.mainIP, device.mainPort, device.enable);
            CommFN<DevInfo> fn = new CommFN<DevInfo>();
            return fn.ExecFN(strSql);
        }

        public bool UpdatNtpServer(string ntp)
        {
            string strSql = "UPDATE devinfo SET ntpserver='" + ntp + "'";
            CommFN<DevInfo> fn = new CommFN<DevInfo>();
            return fn.ExecFN(strSql);
        }

        public bool InsertIncar(Incarevents incarevents)
        {
            try
            {
                string strSql = string.Format("INSERT into incarevents (UUID,credenctype,operatetype,cmdtime,result,reason,ttrAMA2_data,ttrAMA3_data) VALUES('{0}',{1},{2},'{3}','{4}','{5}','{6}','{7}')"
                    , incarevents.UUID, incarevents.credenctype, incarevents.operatetype, incarevents.cmdtime, incarevents.result, incarevents.reason, incarevents.ttrAMA2_data, incarevents.ttrAMA3_data);
                CommFN<Incarevents> fn = new CommFN<Incarevents>();
                return fn.ExecFN(strSql);
            }
            catch
            {
                return false;
            }
        }

        public bool InsertOutcar(Outcarevents outcarevents)
        {
            try
            {
                string strSql = string.Format("INSERT into outcarevents(cmd,cmdtime,result,reason,ttrAMA1_data,ttrAMA3_data)VALUES('{0}','{1}','{2}','{3}','{4}','{5}')"
                    , outcarevents.cmd, outcarevents.cmdtime, outcarevents.result, outcarevents.reason, outcarevents.ttrAMA1_data, outcarevents.ttrAMA3_data);
                CommFN<Incarevents> fn = new CommFN<Incarevents>();
                return fn.ExecFN(strSql);
            }
            catch
            {
                return false;
            }
        }

        public bool InsertModeRecord(ModeRecord modeRecord)
        {
            try
            {
                string strSql = string.Format("INSERT into workmoderecord(workmode,updatetime,reason)VALUES({0},'{1}','{2}')"
                    , modeRecord.workmode, modeRecord.updatetime, modeRecord.reason);
                CommFN<ModeRecord> fn = new CommFN<ModeRecord>();
                return fn.ExecFN(strSql);
            }
            catch
            {
                return false;
            }
        }

        public bool InsertEquipment(Equipment equipment)
        {
            try
            {
                string strSql = string.Format("INSERT into equipment_operation(devtype,state,actiontime,reason)VALUES('{0}',{1},'{2}','{3}')"
                    , equipment.devtype, equipment.state, equipment.actiontime, equipment.reason);
                CommFN<Equipment> fn = new CommFN<Equipment>();
                return fn.ExecFN(strSql);
            }
            catch
            {
                return false;
            }
        }

        public List<AuthMode> QueryAuthMode()
        {
            string strSql = "SELECT * from workmode";
            CommFN<AuthMode> fn = new CommFN<AuthMode>();
            return fn.GetDataList(strSql);
        }

        public bool UpdateAuthMode(DevInfo device)
        {
            try
            {
                string strSql = string.Format("UPDATE devinfo SET authmode={0} WHERE ID={1}", device.authmode, device.ID);
                CommFN<Equipment> fn = new CommFN<Equipment>();
                return fn.ExecFN(strSql);
            }
            catch
            {
                return false;
            }
        }

        public MqttInfo QueryMqttInfo()
        {
            string strSql = "SELECT * from mqtt_info limit 0,1";
            CommFN<MqttInfo> fn = new CommFN<MqttInfo>();
            return fn.GetData(strSql);
        }

        public bool UpdateMqttInfo(MqttInfo mqttInfo)
        {
            string strSql = string.Format("UPDATE mqtt_info SET version='{0}',line='{1}',station='{2}',message_type='{3}',iot_node_id='{4}',device_id='{5}',mqtt_server='{6}',mqtt_port={7},mqtt_uname='{8}',mqtt_pwd='{9}',ele_type={10},number='{11}' WHERE ID={12}", mqttInfo.version, mqttInfo.line, mqttInfo.station, mqttInfo.message_type, mqttInfo.iot_node_id, mqttInfo.device_id, mqttInfo.mqtt_server, mqttInfo.mqtt_port, mqttInfo.mqtt_uname, mqttInfo.mqtt_pwd, mqttInfo.ele_type, mqttInfo.number, mqttInfo.ID);
            CommFN<DevInfo> fn = new CommFN<DevInfo>();
            return fn.ExecFN(strSql);
        }

        public PZMqttInfo QueryPZMqttInfo()
        {
            string strSql = "SELECT * from mqtt_info limit 0,1";
            CommFN<PZMqttInfo> fn = new CommFN<PZMqttInfo>();
            return fn.GetData(strSql);
        }

        public bool UpdatePZMqttInfo(PZMqttInfo mqttInfo)
        {
            string strSql = string.Format("UPDATE mqtt_info SET mqtt_server='{0}',mqtt_port={1},mqtt_uname='{2}',mqtt_pwd='{3}' WHERE ID={4}", mqttInfo.mqtt_server, mqttInfo.mqtt_port, mqttInfo.mqtt_uname, mqttInfo.mqtt_pwd, mqttInfo.ID);
            CommFN<DevInfo> fn = new CommFN<DevInfo>();
            return fn.ExecFN(strSql);
        }

    }
}
